package cn.psvmc.cxzapi.controller.store;

import cn.psvmc.cxzapi.model.common.DelEntity;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.store.TAddress;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.store.TAddressService;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 地址管理
 * @Author auto
 * @Date 2024-08-15
 */
@RestController
@RequestMapping("/address")
public class TAddressController {

    @Resource
    private TAddressService tAddressService;

    /**
    * @Author auto 2024-08-15 新增地址表
    * @Param tAddress 地址表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TAddress tAddress) {
        ToolUtils.validation(new Object[]{
                tAddress.getAddressType(), "地址类型", 1
                , tAddress.getAddress(), "地址", 2
                , tAddress.getStoreid(), "商家id", 1
        });
        int addResult = tAddressService.add(tAddress);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
    * @Author auto 2024-08-15 更新地址表
    * @Param tAddress 地址表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TAddress tAddress) {
        ToolUtils.validation(new Object[]{tAddress.getAddressid(), "id", 1, tAddress.getRversion(), "版本", 1});
        int updateResult = tAddressService.edit(tAddress);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-15 删除地址表
    * @Param entity
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        int delResult = tAddressService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
     * @Description 管理列表
     * @Date 2024/8/15 14:39
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list")
    public Result list(@RequestBody SearchEntity entity) {
        ToolUtils.validation(new Object[]{entity.getStoreid(), "商家id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", tAddressService.listAll(entity));
    }

    /**
     * @Description 选择列表
     * @Date 2024/8/15 14:39
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_sel")
    public Result listForSel(@RequestBody SearchEntity entity) {
        ToolUtils.validation(new Object[]{entity.getStoreid(), "商家id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", tAddressService.listForSel(entity));
    }
}
